package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpTrainAddDTO;
import org.jsola.hr.dto.EmpTrainUpdateDTO;
import org.jsola.hr.query.EmpTrainQuery;
import org.jsola.hr.service.IEmpTrainService;
import org.jsola.hr.vo.EmpTrainListVO;
import org.jsola.hr.vo.EmpTrainVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpTrainController")
@RequestMapping(HrConstants.API_PATH + "/v1/empTrains")
public class EmpTrainController implements IEmpTrainControllerAPI {

    @Autowired
    private IEmpTrainService empTrainService;

    @Override
    @PostMapping
    public Result<EmpTrainVO> save(@Valid @RequestBody EmpTrainAddDTO empTrainAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工培训记录权限校验

        return Result.success(
                empTrainService.save(empTrainAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpTrainUpdateDTO empTrainUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工培训记录权限校验

        int count = empTrainService.update(empTrainUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empTrainId}")
    public Result<Integer> delete(@PathVariable String empTrainId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工培训记录权限校验

        int count = empTrainService.deleteByIds(tokenUser, empTrainId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empTrainIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工培训记录权限校验

        int count = empTrainService.deleteByIds(tokenUser, empTrainIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empTrainId}")
    public Result<EmpTrainVO> findById(@PathVariable String empTrainId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工培训记录权限校验

        return Result.success(
            empTrainService.selectById(empTrainId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpTrainListVO>> listAll(@Valid EmpTrainQuery empTrainQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工培训记录权限校验

        return Result.success(
            empTrainService.select(empTrainQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpTrainListVO>> page(@Valid EmpTrainQuery empTrainQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empTrainService.selectPage(empTrainQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/deleteEmpTrain/{empTrainId}")
    public Result<Integer> deleteEmpTrain(@PathVariable String empTrainId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工培训经历权限校验

        Integer count = empTrainService.deleteEmpTrain(tokenUser, empTrainId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

